package com.business.service.system.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.UUID;

import org.springframework.stereotype.Service;
import com.business.commons.encryption.AES128Utils;
import com.business.commons.uniqueId.IdGen;
import com.business.service.system.TokenService;
import com.business.commons.baseUtil.MD5Util;
import com.business.commons.date.TimeUtil;

@Service
public class TokenServiceImpl implements TokenService{
	/**
	 * 盐值 必须为16位
	 */
	private String encryKey = "!@#$%^GHTR)(*&*(";
	private final String formatDate = "yyMMddHHmmss";
	private final IdGen idGen = IdGen.get();
	private final MD5Util md5 = MD5Util.get();
	
	
	
	/**
	 * 获取加密token
	 * @return
	 */
	@Override
	public String getTokenEncryption() {
		GregorianCalendar gc=new GregorianCalendar();
		gc.add(Calendar.MINUTE, 30);
		AES128Utils.init(encryKey);
		String token = TimeUtil.get().formatDate(gc.getTime(), formatDate)+"-"+idGen.nextId();
		String tokenEnc = AES128Utils.encrypt(token);
		return tokenEnc;
	}

	/**
	 * 获取解密token的值
	 * @param token
	 * @return
	 */
	@Override
	public String getTokenDecrypt(String token) {
		AES128Utils.init(encryKey);
		String tokenmw = AES128Utils.decrypt(token);
		return tokenmw;
	}

	/**
	 * 判断token解密值是否有效
	 * @param token
	 * @return
	 */
	@Override
	public boolean checkTokenDecrypt(String token) {
		AES128Utils.init(encryKey);
		String tokenmw = AES128Utils.decrypt(token);
		if(tokenmw == null){
			return false;
		}
		//验证token是否过期
		else{
			String[] tokenArr = tokenmw.split("-");
			Date lastDateTime = TimeUtil.get().parseDate(formatDate, tokenArr[0]);
			if(new Date().before(lastDateTime)){//token中的时间在当前时间之后返回true
				return true;
			}
		}
		return false;
	}

	
	public static void main(String []args) {
		TokenService tokenService = new TokenServiceImpl();
		/*String miw = tokenService.getTokenEncryption();
		boolean result = tokenService.checkTokenDecrypt(miw);
		String mingw = tokenService.getTokenDecrypt(miw);
		System.out.println("密文："+miw + " 验证结果："+result +" 明文："+mingw);*/
		int num = 10;
		Long begin = System.nanoTime();
		final IdGen idGen = IdGen.get();
		for(int i = 0 ;i < num;i++) {
			String miw = tokenService.getTokenEncryption();
			boolean result = tokenService.checkTokenDecrypt(miw);
			String mingw = tokenService.getTokenDecrypt(miw);
			Date d = new Date();
			Date lastDateTime = TimeUtil.get().parseDate("yyMMddHHmmss", mingw.split("-")[0]);
			d.after(lastDateTime);
			System.out.println("now:"+d.toLocaleString() + " lastDateTime:" + lastDateTime.toLocaleString()+" "+result);
			//System.out.println("密文："+miw + " 验证结果："+result +" 明文："+mingw);
		}
		Long end = System.nanoTime();
		System.out.println("所需时间："+(end-begin)/1000000.0/num + "毫秒");
		 MD5Util md5 = MD5Util.get();
		 String s = md5.md5Hex16("\"加盐值为nichaibudao!@#$#%#$%#$\"");
		 System.out.println("!!!!!!!!!!!!!!!! md5:" + s + " length:"+s.length());
	}

	/**
	 * 获取加密token
	 * @param idcard 身份证号
	 * @return
	 */
	@Override
	public String getTokenEncryption(String idcard) {
		GregorianCalendar gc=new GregorianCalendar();
		gc.add(Calendar.MINUTE, 30);
		AES128Utils.init(encryKey);
		String token = TimeUtil.get().formatDate(gc.getTime(), formatDate)+"-" + idcard;
		String tokenEnc = AES128Utils.encrypt(token);
		return tokenEnc;
	}

	@Override
	public void setEncryKey(String key) {
		if(null != key && "".equals(key)) {
			this.encryKey = this.md5.md5Hex16(key+"加盐值为nichaibudao!@#$#%#$%#$");
		}
	}
}
